<?php

class Common_DataTable_View_Csv implements Common_DataTable_View {

    public function publish($arr_data, $arr_headers, $arr_footers, $arr_properties, $arr_options = array()) {
        
        $str_output = null;
        
        if(!empty($arr_data)) {

            $obj_template = new Common_Template();

            $obj_template->assign(array(
                'arr_headers'    => $arr_headers,
                'arr_data'       => $arr_data,
                'arr_footers'    => $arr_footers,
                'arr_properties' => $arr_properties,
                'arr_options'    => $arr_options
            ));

           
            $str_output = $obj_template->render('datatable/csv.phtml');
            
        }

        return $str_output;
        
    }
    
    static public function generateRow($int_row_index, $arr_row, $arr_columns) {
        
        $arr_output = array();
        
        $int_column_index = 0;
        foreach($arr_columns as $str_key => $mix_header) {
    
            $arr_header = (array) $mix_header;
            
            //------------------------------------------------------------------
            
            // determine the data source, which defaults to the column key
            $str_source_key = isset($arr_header['source']) ? $arr_header['source'] : $str_key;
            
            // determine the data value
            $mix_value = isset($arr_row[$str_source_key]) ? $arr_row[$str_source_key] : null;
    
            // determine the data type
            $enm_type = isset($arr_header['type']) ? $arr_header['type'] : 'string';
            
            //------------------------------------------------------------------
    
            if($enm_type != 'special') {
            
                if(is_null($mix_value)) {
        
                    $mix_value = null;
        
                    if(isset($arr_header['default'])) {
                        $mix_value = $arr_header['default'];
                    }
        
                } else {
                    
                    if(isset($arr_header['format'])) {
        
                        $str_format_type = $arr_header['format'];
        
                        switch($str_format_type) {
                        case 'link':
                        case 'checkbox':
                            break;
                        default:
                            $arr_format_options = isset($arr_header['format_options'])
                                ? $arr_header['format_options']
                                : array();
        
                            $mix_value = Utility_FormatFunctions::format_value($arr_header['format'], $mix_value, $arr_row, $arr_format_options);
                            break;
                        }
        
                    }
                    
                }
            
            
            } else {
                $mix_value = null;    
            }
            
            $arr_output[] = $mix_value;
                
            
            $int_column_index++;
        }
        
        
        return $arr_output;
        
    }
    
}
